package com.example.forumsystem.controller;

import com.example.forumsystem.common.AppResult;
import com.example.forumsystem.common.ResultCode;
import com.example.forumsystem.config.AppConfig;
import com.example.forumsystem.exception.ApplicationException;
import com.example.forumsystem.model.Article;
import com.example.forumsystem.model.ArticleReply;
import com.example.forumsystem.model.User;
import com.example.forumsystem.services.IArticleReplyService;
import com.example.forumsystem.services.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


@Api(tags = "回复相关接口")
@Slf4j
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {

    @Resource
    IArticleReplyService articleReplyService;

    @Resource
    IArticleService articleService;

    @ApiOperation("创建回复")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("文章id") @RequestParam("articleId") @NonNull Long articleId,
                            @ApiParam("文章回复") @RequestParam("content") @NonNull String content) {

        // 获取登入用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);

        // 校验用户权限
        if(user == null || user.getDeleteState() == 1 || user.getState() == 1) {
            // 打印日志
            log.info(ResultCode.FAILED_USER_BANNED.toString());
            // 返回异常
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        // 获取文章对象
        Article article = articleService.selectById(articleId);
        // 校验文章状态
        if(article == null || article.getDeleteState() == 1 || article.getState() == 1) {
            // 打印日志
            log.info(ResultCode.FAILED_ARTICLE_BANNED.toString());
            // 返回异常
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }

        // 构建回复对象
        ArticleReply articleReply = new ArticleReply();
        articleReply.setPostUserId(user.getId());
        articleReply.setArticleId(articleId);
        articleReply.setContent(content);

        // 调用service,新增回复
        articleReplyService.create(articleReply);

        return AppResult.success();
    }


    @GetMapping("/getReplies")
    @ApiOperation("获取回复列表")
    private AppResult<List<ArticleReply>> getRepliesByArticleId(@ApiParam("文章id") @RequestParam("articleId") @NonNull Long articleId) {

        // 获取文章信息
        Article article = articleService.selectById(articleId);
        // 校验文章合法性
        if(article == null || article.getState() == 1 || article.getDeleteState() == 1) {
            // 打印日志
            log.info(ResultCode.FAILED_ARTICLE_BANNED.toString());
            // 返回异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED));
        }
        List<ArticleReply> result = articleReplyService.selectByArticleId(articleId);


        return AppResult.success(result);
    }
}
